#include "BBConstroller.h"
#include "BBCommander.h"

BBController::BBController( void )
{

}

BBController::~BBController( void )
{

}

void BBController::addCommander( int tag,BBCommander* command,bool isCommit /*= false*/ )
{
	if(command == NULL)
		return;
	m_pCommandList[tag] = command;
	if(isCommit)
		command->execute();
}

void BBController::removeCommand( int tag )
{
	if(m_pCommandList.empty())
		return;	
	pCommandIterator iter;
	for(iter =m_pCommandList.begin();iter!=m_pCommandList.end();iter++)
	{
		if((*iter).second->getCommandTag() == tag)
		{
			m_pCommandList.erase(iter);
			break;
		}
	}
}

void BBController::executeCommand( int tag,float delay /*=0.0f*/ )
{
	if(m_pCommandList.empty())
		return;
	BBCommander* cmd = getCommanderByTag(tag);
	if(cmd == NULL)
		return;
	if(delay)
		cmd->execute();
}

void BBController::clear()
{
	m_pCommandList.clear();
}

BBCommander* BBController::getCommanderByTag( int tag )
{
	BBCommander* cmd = NULL;
	if(m_pCommandList.find(tag) != m_pCommandList.end())
	{
		cmd = m_pCommandList[tag];
	}
	return cmd;
}
